//
//  PosterCollectionViewCell.m
//  HuiWenMovieApp
//
//  Created by ios on 15/7/24.
//  Copyright (c) 2015年 ios. All rights reserved.
//

#import "PosterCollectionViewCell.h"
#import "UIViewExt.h"

#define kwth [UIScreen mainScreen].bounds.size.width
#define kheight [UIScreen mainScreen].bounds.size.height
@implementation PosterCollectionViewCell




- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    CGRect temp = frame;
    CGFloat height = 0.9 *temp.size.height;
    CGFloat width = 0.9 *temp.size.width;
    
    NSArray *dic = [[NSBundle mainBundle]loadNibNamed:@"PosterCollectionView" owner:self options:nil];
    
    if (self)
    {
        _poster = [[UIImageView alloc]initWithFrame:CGRectMake((self.width- width)/2, (self.height - height)/2, width,height )];
        [self.contentView addSubview:_poster];
        _detailView = [dic lastObject];
        _detailView.frame = CGRectMake((self.width- width)/2, (self.height - height)/2, width,height );
        [self.contentView addSubview:_detailView];
        _poster.hidden = NO;
        _detailView.hidden = YES;
    }

    return  self;
}

- (void)setModel:(MovieModel *)model
{
    _model = model;
    [self setNeedsLayout];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    NSString *urlOfImage = [_model.images objectForKey:@"large"];
    [_poster sd_setImageWithURL:[NSURL URLWithString:urlOfImage]];
    
    urlOfImage = [_model.images objectForKey:@"medium"];
    [_detailView.imageView sd_setImageWithURL:[NSURL URLWithString:urlOfImage]];
    _detailView.chineseTitle.text = _model.title;
    _detailView.englishTitle.text = _model.englishTitle;
    _detailView.year.text = _model.year;
    _detailView.rating.text = [NSString stringWithFormat:@"%0.1f",_model.average];
    _detailView.starView.average = _model.average;
}
- (void)flipView:(UIView *)view isLeft:(BOOL)isleft
{
    UIViewAnimationOptions option = isleft?UIViewAnimationOptionTransitionFlipFromLeft:UIViewAnimationOptionTransitionFlipFromRight;
    
    [UIView transitionWithView:view duration:0.3 options:option animations:nil completion:nil];
    
}

- (void)flip
{

    [self flipView:_poster isLeft:!_poster.hidden];
    [self flipView:_detailView isLeft:!_poster.hidden];
    _poster.hidden = !_poster.hidden;
    _detailView.hidden = !_detailView.hidden;


}



@end
